﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Process.Entities;
using Process.BLL;
using System.Text.RegularExpressions;
using KZ.Utilities;
using Process;

namespace Provider
{
    public partial class ProductQuantity : System.Web.UI.Page
    {
        Regex regex = new Regex(@"(.*)(\?op\=)(.*)");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckQuantity();
                LoadTransaction();
            }
        }

        protected void btnUpdateInventory_Click(object sender, EventArgs e)
        {
            string partnerID = Request.QueryString["p"];

            PartnerENT partner = PartnerBLL.GetPartnerByID(partnerID);
            UpdateInventory(partner.WS5);
        }

        private void CheckQuantity()
        {
            string transID = Request.QueryString["t"];
            string partnerID = Request.QueryString["p"];

            PartnerENT partner = PartnerBLL.GetPartnerByID(partnerID);
            string ws = regex.Replace(partner.WS4, "$1");
            if (!ws.EndsWith("?wsdl")) { ws += "?wsdl"; }
            WebServiceInvoker invoker = new WebServiceInvoker(new Uri(ws));

            string methodName = regex.Replace(partner.WS4, "$3");

            object[] obj = { Global.PartnerSID, transID };
            int result = invoker.InvokeMethod<int>(invoker.AvailableServices[0], methodName, obj);
            if (result >= 0)
            {
                txtQuantity.Text = result.ToString();
            }
            else
            {
                lblMessage.Text = "Kiểm tra tồn kho không thành công.";
                message.Attributes.Add("class", "notification error png_bg message");
                message.Style["display"] = "block";
            }
        }

        private void LoadTransaction()
        {
            string transID = Request.QueryString["t"];

            TransactionENT trans = TransactionBLL.GetTransactionByID(transID);
            txtProductName.Text = trans.ProductName;
            txtPartnerName.Text = trans.PartnerName;
            txtUnitPice.Text = Utility.CurrencyParse(trans.ProductPrice, Global.Culture);
            txtMinQuantity.Text = trans.MinQuantity.ToString();
            txtAddedQuantity.Text = trans.NextTransQuantity.ToString();
            txtTotalPrice.Text = Utility.CurrencyParse(trans.ProductPrice * trans.NextTransQuantity, Global.Culture);
        }

        private void UpdateInventory(string wsLink)
        {
            string transID = Request.QueryString["t"];

            string ws = regex.Replace(wsLink, "$1");
            if (!ws.EndsWith("?wsdl")) { ws += "?wsdl"; }
            WebServiceInvoker invoker = new WebServiceInvoker(new Uri(ws));

            string methodName = regex.Replace(wsLink, "$3");

            int addedQuantity = Utility.IntParse(txtAddedQuantity.Text);
            decimal unitPrice = Utility.DecimalParse(txtUnitPice.Text);
            decimal transportPrice = Utility.DecimalParse(txtTransportPrice.Text);
            decimal totalPrice = unitPrice * addedQuantity + transportPrice;

            object[] obj = { Global.PartnerSID, transID, addedQuantity, unitPrice, transportPrice, totalPrice };

            int result = invoker.InvokeMethod<int>(invoker.AvailableServices[0], methodName, obj);
            if (result >= 0)
            {
                lblMessage.Text = "Cập nhật kho thành công.";
                message.Attributes.Add("class", "notification success png_bg message");
                CheckQuantity();

                OrderENT order = new OrderENT
                {
                    OrderID = Guid.NewGuid().ToString(),
                    OrderDate = DateTime.Now,
                    TransID = transID,
                    Quantity = addedQuantity,
                    UnitPrice = unitPrice,
                    TransportPrice = transportPrice,
                    GrandTotal = totalPrice,
                    OrderStatus = OrderStatus.Draft.ToString()
                };

                OrderBLL orderBLL = new OrderBLL();
                orderBLL.InsertOrder(order);
            }
            else
            {
                lblMessage.Text = "Cập nhật kho không thành công.";
                message.Attributes.Add("class", "notification error png_bg message");
            }
            message.Style["display"] = "block";
        }
    }
}